package com.longxi.tianjintianbao.ui.activity;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.listener.SearchListener;
import com.longxi.tianjintianbao.model.Search;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.presenter.repair.PersonnelAtPresenter;
import com.longxi.tianjintianbao.ui.view.repair.IPersonnelAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.widget.SearchView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;

public class PersonnelActivity extends BaseActivity<IPersonnelAtView, PersonnelAtPresenter> implements IPersonnelAtView {

    @BindView(R.id.etSearch)
    EditText mEtSearch;
    @BindView(R.id.searchView)
    SearchView mSearchView;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_personnel;
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter.setBar();
        mPresenter.initAdapter();
        mPresenter.getPersonnel();
        SearchListener searchListener = new SearchListener() {
            @Override
            public void onSearch(String keyword) {
                if("".equals(keyword)){
                    mPresenter.groupBys();
                }else{
                    mPresenter.search(keyword);
                }
            }
        };
        mSearchView.setDate(new Search("请输入人员姓名"), searchListener);
    }

    @Override
    public void initListener() {
        super.initListener();
        mTvRight.setOnClickListener(v -> mPresenter.hdPersonnel());

        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //回车时返回true拦截事件，不让换行
                if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                    if (TextUtils.isEmpty(mEtSearch.getText().toString().trim())) {
                        MyUtils.showToast("请输入搜索内容");
                    } else {//不为空时才添加标签
                        mPresenter.search(mEtSearch.getText().toString().trim());
                    }
                    return true;
                }
                return false;
            }
        });

        mEtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(TextUtils.isEmpty(mEtSearch.getText().toString().trim())){
                    mPresenter.groupBys();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    protected PersonnelAtPresenter createPresenter() {
        return new PersonnelAtPresenter(this);
    }

    @Override
    public EditText getEtSearch() {
        return mEtSearch;
    }

    @Override
    public SmartRefreshLayout getRefreshLayout() {
        return mRefreshLayout;
    }

    @Override
    public RecyclerView getRvContent() {
        return mRvContent;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
